package com.alongstudio.account.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class DateUtil {
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	static SimpleDateFormat dateFormatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMddHHmmss");
	/**
	 * 将“yyyyMMddHHmmss” 字符串格式化成日期
	 * @param dateStr
	 * @return
	 */
	public static Date getDate2(String dateStr){
		 try
		{
			return dateFormat2.parse(dateStr);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return null;
	
	}
	/**
	 * 将"yyyy-MM-dd"字符串格式化成日期
	 * @param dateStr
	 * @return
	 */
	public static Date getDate(String dateStr)
	{
		 try
		{
			return dateFormat.parse(dateStr);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 将日期格式化成"yyyy-MM-dd"字符串
	 * @param date
	 * @return
	 */
	public static String getDateString(Date date)
	{
		return dateFormat.format(date);
	}
	/**
	 *  将"yyyy-MM-dd HH:mm:ss"字符串格式化成日期
	 * @param dateStr
	 * @return
	 */
	public static Date getDateTime(String dateStr)
	{
		 try
		{
			return dateFormatTime.parse(dateStr);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 将日期格式化成"yyyy-MM-dd HH:mm:ss"字符串
	 * @param date
	 * @return
	 */
	public static String getDateTimeString(Date date)
	{
		return dateFormatTime.format(date);
	}
	/**
	 * 指定日期进行减月份
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceMonth(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-offset);  
		return calendar.getTime();
	}
	
	/**
	 * 指定日期进行减天数
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceDays(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)-offset);//让日期加位移量  
		   
		return calendar.getTime();
	}
	/**
	 * 指定日期进行减小时
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceHours(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-offset);  
		   
		return calendar.getTime();
	}
	/**
	 * 指定日期进行减分钟
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceMinutes(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.MINUTE,calendar.get(Calendar.MINUTE)-offset);  
		return calendar.getTime();
	}
	
	/**
	 * 但指定日期进行加减天数
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addDays(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+offset);//让日期加位移量  
		   
		return calendar.getTime();
	}
	
	/**
	 * 指定日期进行加减小时
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addHours(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)+offset);  
		   
		return calendar.getTime();
	}
	/**
	 * 制定日期减分钟
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addMinutes(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.MINUTE,calendar.get(Calendar.MINUTE)+offset);  
		   
		return calendar.getTime();
	}
	/**
	 * 制定日期减分钟
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addSeconds(Date date,int offset){
		Calendar calendar=Calendar.getInstance();   
		calendar.setTime(date); 		
		calendar.set(Calendar.SECOND,calendar.get(Calendar.SECOND)+offset);  
		   
		return calendar.getTime();
	}
	public static void main(String[] arg)
	{
		Date newDate = new Date();
		System.out.println(newDate);
		System.out.println(addMinutes(newDate, 5));
		testMap();
		
	}
	static void testMap(){
		Map map = new HashMap();
		Date nowDate = new Date();
		for(int i =0;i<10;i++){
			map.put(i, reduceMinutes(nowDate, i));
		}
		Iterator iter = map.entrySet().iterator(); 
		Map newMap = new HashMap();
		while (iter.hasNext()) { 
			System.out.println(map.keySet());
		    Entry entry = (Entry) iter.next(); 
		    Object key = entry.getKey();
		    Date date = (Date)entry.getValue();
		    Date timeAdd = DateUtil.addMinutes(date, 5);
		    System.out.println("key:"+key+"   value:"+date + " add5Min:"+timeAdd +" sysTime:"+ nowDate);
		    if(timeAdd.before(nowDate)){
		    	newMap.put(key, date);
		    	System.out.println(newMap.keySet());
		    }

		}
	}
	
}
